package com.group4.edu.controller;


import com.group4.edu.service.CourseService;
import com.study.util.LogUtils;
import com.study.util.ResponseResult;
import com.study.vo.CourseInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@RestController
@RequestMapping("/edu/course")
@Slf4j
@CrossOrigin
@Api(description = "课程管理")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "添加课程")
    @PostMapping("/add")
    public ResponseResult add(
            @ApiParam(value = "课程信息",required = true)
            @RequestBody CourseInfoVO courseInfoVO){

        LogUtils.info(log,"添加课程",courseInfoVO);
        String courseId = courseService.add(courseInfoVO);
        return new ResponseResult(courseId);
    }

    @ApiOperation(value = "根据id查询课程")
    @GetMapping("getById/{id}")
    public ResponseResult getById(@PathVariable @ApiParam(value = "课程id") String id){
        LogUtils.info(log,"根据id查询课程",id);
        CourseInfoVO courseInfoVO = courseService.getById(id);
        return new ResponseResult(courseInfoVO);
    }

    @ApiOperation(value = "根据id更新销量")
    @PutMapping("/updateBuyCount/{id}")
    public ResponseResult<Void> updateBuyCount(@PathVariable("id") @ApiParam(value = "课程id") String id){
        LogUtils.info(log,"更新销量",id);
        courseService.updateBuyCount(id);
        return ResponseResult.SUCCESS;
    }

    @ApiOperation(value = "更新course数据")
    @PutMapping("/update-course-info")
    public ResponseResult updateById(
            @ApiParam(name = "CourseInfo", value = "课程基本信息", required = true)
            @RequestBody CourseInfoVO courseInfoVO){
        LogUtils.info(log,"更新课程",courseInfoVO);
        courseService.updateCourseInfoById(courseInfoVO);
        return ResponseResult.SUCCESS;
    }
}

